package com.testes.umov.view;

import java.util.ArrayList;
import java.util.Hashtable;

import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.testes.umov.R;
import com.testes.umov.constants.Constants;
import com.testes.umov.model.RgbColor;
import com.testes.umov.model.TestData;
import com.testes.umov.screens.ScreensTestsController;
import com.testes.umov.util.ControllerListener;

public class ScreenTestsActivity extends Activity implements ControllerListener{

	Button goButton;
	TextView resultTextView, screenCountTextView;	
	ScreensTestsController controller;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_screen_tests);
		
		resultTextView = (TextView)findViewById(R.id.tests_screen_activity_resultTextView);
		screenCountTextView = (TextView)findViewById(R.id.tests_screen_activity_numberOfScreensTextView);
		goButton = (Button)findViewById(R.id.tests_screen_goButton);
		
		goButton.setOnClickListener(new OnClickListener() 
		{			
			@Override
			public void onClick(View v) 
			{
				collectCollorsAndDoTests();
			}
		});
	}
	
	private void collectCollorsAndDoTests() {
		controller = new ScreensTestsController(this, (ControllerListener)this);
		EditText view;
		int red, green, blue, iconColor;
		ArrayList<RgbColor> listColor = new ArrayList<RgbColor>();
		
		// Coleta cores do header
		view = (EditText)findViewById(R.id.headerRed);
		red = Integer.parseInt((String) view.getText().toString());
		view = (EditText)findViewById(R.id.headerGreen);
		green = Integer.parseInt((String) view.getText().toString());
		view = (EditText)findViewById(R.id.headerBlue);
		blue = Integer.parseInt((String) view.getText().toString());
		
		if(red < 0 || green < 0 || blue < 0 || red > 255 || green > 255 || blue > 255) {
			Toast toast = Toast.makeText(this, "Invalid values for header color", Toast.LENGTH_LONG);
			toast.show();
			return;
		}		
		listColor.add(new RgbColor(red, green, blue, "header"));
		
		// Coleta cores do footer
		view = (EditText)findViewById(R.id.footerRed);
		red = Integer.parseInt((String) view.getText().toString());
		view = (EditText)findViewById(R.id.footerGreen);
		green = Integer.parseInt((String) view.getText().toString());
		view = (EditText)findViewById(R.id.footerBlue);
		blue = Integer.parseInt((String) view.getText().toString());
		if(red < 0 || green < 0 || blue < 0 || red > 255 || green > 255 || blue > 255) {
			Toast toast = Toast.makeText(this, "Invalid values for footer color", Toast.LENGTH_LONG);
			toast.show();
			return;
		}
		listColor.add(new RgbColor(red, green, blue, "footer"));
				
		// Coleta cores do fundo
		view = (EditText)findViewById(R.id.backgroundRed);
		red = Integer.parseInt((String) view.getText().toString());
		view = (EditText)findViewById(R.id.backgroundGreen);
		green = Integer.parseInt((String) view.getText().toString());
		view = (EditText)findViewById(R.id.backgroundBlue);
		blue = Integer.parseInt((String) view.getText().toString());
		if(red < 0 || green < 0 || blue < 0 || red > 255 || green > 255 || blue > 255) {
			Toast toast = Toast.makeText(this, "Invalid values for background color", Toast.LENGTH_LONG);
			toast.show();
			return;
		}
		listColor.add(new RgbColor(red, green, blue, "background"));
		LinearLayout layout = (LinearLayout)findViewById(R.id.tests_screen_main_layout);
		layout.setBackgroundColor(Color.rgb(red, green, blue));
		
		// Coleta cores de texto
		view = (EditText)findViewById(R.id.textRed);
		red = Integer.parseInt((String) view.getText().toString());
		view = (EditText)findViewById(R.id.textGreen);
		green = Integer.parseInt((String) view.getText().toString());
		view = (EditText)findViewById(R.id.textBlue);
		blue = Integer.parseInt((String) view.getText().toString());
		if(red < 0 || green < 0 || blue < 0 || red > 255 || green > 255 || blue > 255) {
			Toast toast = Toast.makeText(this, "Invalid values for text color", Toast.LENGTH_LONG);
			toast.show();
			return;
		}
		listColor.add(new RgbColor(red, green, blue, "text"));		
		
		// Coleta cores de outros textos
		view = (EditText)findViewById(R.id.otherTextRed);
		red = Integer.parseInt((String) view.getText().toString());
		view = (EditText)findViewById(R.id.otherTextGreen);
		green = Integer.parseInt((String) view.getText().toString());
		view = (EditText)findViewById(R.id.otherTextBlue);
		blue = Integer.parseInt((String) view.getText().toString());
		if(red < 0 || green < 0 || blue < 0 || red > 255 || green > 255 || blue > 255) {
			Toast toast = Toast.makeText(this, "Invalid values for otherText color", Toast.LENGTH_LONG);
			toast.show();
			return;
		}
		listColor.add(new RgbColor(red, green, blue, "othertext"));
		
		// Coleta cor do icone
		view = (EditText)findViewById(R.id.textViewIconsColor);
		iconColor = Integer.parseInt((String) view.getText().toString());
		if(iconColor >4 || iconColor < 1) {
			Toast toast = Toast.makeText(this, "Invalid value for icons color", Toast.LENGTH_LONG);
			toast.show();
			return;
		}
		
		controller = new ScreensTestsController(this, this);
		controller.doTestsWithColors(listColor, iconColor);
	}

	@Override
	public void onTestsFinished(Hashtable<String, Object> results) {
		Double spentBattery = (Double)results.get(Constants.BATTERY_SPENT);
		Integer screenCount = (Integer)results.get(Constants.SCREEN_COUNT); 
		resultTextView.setText(spentBattery.toString());
		screenCountTextView.setText(screenCount.toString());
		Toast toast = Toast.makeText(this, Constants.FINISHED_TEST + "(ScreenTests)", Toast.LENGTH_LONG);
		toast.show(); 
	}

	@Override
	public void onPartialTestFinished(TestData testData) {
		// TODO Auto-generated method stub
		
	}
}
